package com.xu.t1.middleware.threadallocation;

import com.xu.t1.mapper.UploadLogMapper;
import com.xu.t1.middleware.online.OnlineUserManager;
import com.xu.t1.model.ThreadAllocationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class ThreadHandler4 implements ThreadAllocationHandler {

    @Value("${app.server-id}")
    private String serverId;

    private ThreadAllocationHandler nextHandler;

    @Autowired
    private UploadLogMapper uploadLogMapper;

    @Autowired
    private OnlineUserManager onlineUserManager;

    @Override
    public boolean handle(ThreadAllocationContext context) {


        // 简略 可根据计算实时写入能力 限定预期单位时间
        int totalSize = context.getTotalSize();
        int threadsByData = (int) Math.ceil((double) totalSize / 10000);

        context.setAllocatableThreads(Math.min(context.getAllocatableThreads(), threadsByData));

        return true;

    }

    @Override
    public ThreadAllocationHandler setNext(ThreadAllocationHandler nextHandler) {
        this.nextHandler = nextHandler;
        return nextHandler;
    }
}